﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using NHibernate.Context;
using NHibernate;
using DomainModel.Concrete;
using Elmah;
using System.Net;
using System.Web.Mvc;

namespace WebUI
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("elmah.axd");
            routes.IgnoreRoute("favicon.ico");
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            
             
            routes.MapRoute(
                null,
                "events/", // Only matches the empty URL (i.e. ~/)
                new { controller = "Event", action = "Index", page = 1 }
                );


            routes.MapRoute(
                null,
                "events/page/{page}",
                new { controller = "Event", action = "Index" },
                new { page = @"\d+" } // Constraints: page must be numerical
                );

            routes.MapRoute(
               null,
               "events/{id}/{title}",
               new { controller = "Event", action = "Details", id = @"\d+", title = (string)null }
               );

            routes.MapRoute(
              null,
              "tags/{TagName}/",
              new { controller = "Tag", action = "List", TagName = (string)null }
              );


            routes.MapRoute(
               null,
               "users/{id}/{title}",
               new { controller = "User", action = "Details", id = @"\d+", title = (string)null }
               );


            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
                );



        }



        ////http://stackoverflow.com/questions/762193/block-404-favico-error-in-elmah
        //void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
        //{
        //    if (((HttpException)e.Exception.GetBaseException()).GetHttpCode() == (int)HttpStatusCode.NotFound
        //       && ((HttpContext)e.Context).Request.Path == "/favicon.ico")
        //    {
        //        e.Dismiss();
        //    }
        //}

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            ManagedWebSessionContext.Bind(HttpContext.Current, SessionManager.SessionFactory.OpenSession());
        }

        protected void Application_EndRequest(object sender, EventArgs e)
        {
            CloseSession();
        }

        private static void CloseSession()
        {
            ISession session = ManagedWebSessionContext.Unbind(HttpContext.Current, SessionManager.SessionFactory);
            if (session != null)
            {
                try
                {
                    if (session.Transaction != null && session.Transaction.IsActive)
                    {
                        session.Transaction.Rollback();
                    }
                    else
                    {
                        session.Flush();
                    }
                }
                finally
                {

                    session.Close();
                }
            }
        }
    }
}